<!-- BEGIN PAGE HEADING ROW -->
<div class="row">
	<div class="col-lg-12">
		<!-- BEGIN BREADCRUMB -->
		<div class="breadcrumbs">
			<ul class="breadcrumb">
				<li> <a href="<?= $this->webroot.'admin' ?>">Trang chủ</a> </li>
				<li class="active">Danh sách sản phẩm</li>
			</ul>
		</div>
		<!-- END BREADCRUMB -->

		<div class="page-header">
			<h1>Danh Sách Sản Phẩm</h1>
		</div>

		<?php echo $this->element('layout/layout-options')?>

	</div><!-- /.col-lg-12 -->
</div><!-- /.row -->
<!-- END PAGE HEADING ROW -->


<div class="row">
	<div class="col-lg-12">
		<?php echo $this->Session->flash(); ?>
		<div class="row row-top">
			<h5 class="fl">Danh mục cha</h5>
			<div class="col-lg-3">
				<?php echo $this->Form->input($als.'.parent_id', array( 'label' => false, 'div' => false, 'class' => 'form-control', 'options' => $parent_categories, 'default' => $cate_id) )?>
			</div>

			<h5 class="fl">Danh mục</h5>
			<div class="col-lg-3">
				<?php echo $this->Form->input($als.'.category_id', array( 'label' => false, 'div' => false, 'class' => 'form-control', 'options' => $categories, 'default' => $category_id) )?>
			</div>
			<?php if( count($categories) > 0) :?>
			<a href="<?= $this->webroot.'admin' ?>/products/add/<?= $cate_id.'/'.$category_id ?>" class="btn btn-primary"><i class="fa fa-plus"></i>Thêm mới</a>
			<?php endif; ?>
		</div>

		<table class="table table-bordered table-striped table-hover tc-table table-primary footable" data-page-size="2">
			<thead>
				<tr>
					<th>STT</th>
					<th>Tên</th>
					<th>Miêu Tả</th>
					<th></th>
				</tr>
			</thead>
			<tbody>
				<?php if(count($items) > 0):?>
				<?php foreach($items as $k => $v):?>
				<tr>
					<td style="width:5%" align="center">
						<a href="#" data-pid="<?php echo $v[$als]['id']?>" class="_sort" style="display:block;"><?php echo $v[$als]['sort'];?></a>
						<input type="text" class="hide" style="width:32px; text-align: center;" data-sort="<?php echo $v[$als]['sort'];?>" value="<?php echo $v[$als]['sort'];?>" />
					</td>
					<td style="width:20%"><?php echo $v[$als]['name'];?></td>
					<td><?php echo $v[$als]['desc'];?></td>
					<td class="col-medium center hidden-xs hidden-sm">
						<div class="action-buttons">
							<a href="<?= $this->webroot.'admin' ?>/products/images/<?= $cate_id.'/'.$category_id ?>/<?= $v[$als]['id']?>" class="tooltip-default" data-placement="top" data-rel="tooltip" title="" data-original-title="Xem hình ảnh"><i class="fa fa-picture-o bigger-130"></i></a>
							<a href="<?= $this->webroot.'admin' ?>/products/add/<?= $cate_id.'/'.$category_id ?>/<?= $v[$als]['id']?>" class="tooltip-green" data-placement="top" data-rel="tooltip" title="" data-original-title="Sửa"><i class="fa fa-edit bigger-130 text-green"></i></a>
							<a href="<?= $this->webroot.'admin' ?>/products/delete/<?= $cate_id.'/'.$category_id?>/<?= $v[$als]['id']?>" class="tooltip-red" data-placement="top" data-rel="tooltip" title="" data-original-title="Xóa"><i class="fa fa-trash-o bigger-130 text-red"></i></a>
						</div>
					</td>
				</tr>
				<?php endforeach; else:?>
				<tr>
					<td colspan="5"><div class="note note-primary">Chưa có sản phẩm</div></td>
				</tr>
				<?php endif; ?>

			</tbody>
			<?php $params = $this->Paginator->params(); if($params['pageCount'] > 1) :?>
			<tfoot>
				<tr>
					<td colspan="5" class="footable-visible">
						<?php echo $this->element('paging'); ?>
					</td>
				</tr>
			</tfoot>
			<?php endif; ?>
		</table>


	</div><!-- /.col-lg-12 -->
</div><!-- /.row -->

<?php
	echo $this->Html->script('plugins/bootbox/bootbox.min');
	echo $this->Html->script('functions');
?>

<script>
$( document ).ready(function($) {
	var $parent_cb = $('#ProductParentId');
	var $ProductCategory = $('#ProductCategoryId');

	// Change category
	$ProductCategory.change(function(event){
		window.location.href = '<?php echo $this->webroot; ?>admin/products/index/' + $parent_cb.val() + '/' + $(this).val();
	});

	$parent_cb.change(function(event){
		getAjax('<?php echo $this->webroot; ?>categories/getListByParent/' + $parent_cb.val(), true, function(resp){
			$parent_cb.closest('.row').find('.btn-primary').remove();
			var $option = '<option>--Chọn danh mục--</option>';
			$.each(resp.cates, function( idx, value ) {
				$option += '<option value="'+value.Category.id+'">'+value.Category.name+'</option>';
            });
			$ProductCategory.html($option);
		});
	});

	// Change sort
	showSort($('._sort'));


	// xoa san pham
	$('.action-buttons .tooltip-red').click(function(event){
		event.preventDefault();
		var href =  $(this).attr('href');
		bootbox.confirm("Bạn có chắc chắn muốn xóa sản phẩm này không?", function(result) {
			if(result) {
				window.location.href = href;
			}
		});
		return false;
	});
});

function showSort($hml) {
	$hml.click(function(event){
		event.preventDefault();

		$('.alert').remove();

		var $span = $(this);

		var value = $span.html();

		var $input = $span.next();

		checkInput($input, value);
		$input.data('sort', value).val(value).attr('value', value).removeClass('hide');
		$span.hide();

		$input.focus();

		$input.focusout(function(event){
			event.preventDefault();
			$('.alert').remove();

			postAjax('<?php echo $this->webroot; ?>admin/products/changeSort', {id:$span.data('pid'), sort:$input.val()}, function(resp){
				var msg = 'Sửa thứ tự thất bại';
				if(resp.code == 'success'){
					$span.html($input.val());
					msg = 'Sửa thứ tự thành công';
					$input.addClass('hide');
					$span.show();
				}

				var $msg = '<div class="alert alert-'+resp.code+'"><button type="button" class="close" data-dismiss="alert" aria-hidden="true"><i class="fa fa-times"></i></button><p><strong>Thành Công:</strong><br> '+msg+'</p></div>';
				$input.closest('table').before($msg);
			}, 'json', true);
		});
	});
}



/**
 * check als
 */
function checkInput($input, value) {
	$input.unbind().keyup(function(){
        if (this.value.match(/[^0-9]/g)) {
            this.value = value;
        }
    });
}


</script>
